using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace MapMaker
{
    public partial class frmExportToPOI : Form
    {
        private frmMain parentForm;
        private string savePath = null;
        private ArrayList route;
        private string fileName = null;

        public frmExportToPOI()
        {
            InitializeComponent();
        }

        private void frmExportToPOI_Load(object sender, EventArgs e)
        {
            parentForm = ((frmMain)this.MdiParent);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = dlgBrowse.ShowDialog(parentForm);

            if (result == DialogResult.OK)
            {
                savePath = dlgBrowse.SelectedPath;
                lblSaveFolder.Text = savePath;
            }
        }

        private void saveFile()
        {
            bool validationError = false;
            StreamWriter writer = null;

            try
            {
                if (string.IsNullOrEmpty(savePath))
                {
                    lblFolder.ForeColor = Color.Red;
                    validationError = true;
                }

                if (string.IsNullOrEmpty(txtPOIName.Text.Trim()))
                {
                    lblPOIName.ForeColor = Color.Red;
                    validationError = true;
                }

                if (!validationError)
                {
                    fileName = txtPOIName.Text;
                    if (!fileName.StartsWith("_"))
                    {
                        fileName = "_" + fileName;
                    }

                    route = parentForm.DirectionsForm.Route;

                    string distanceUnit = parentForm.DirectionsForm.DistanceUnit;
                    string[] line = null;
                    string lineToWrite = null;

                    writer = new StreamWriter(Path.Combine(savePath, fileName), false);
                    writer.WriteLine("#");
                    writer.WriteLine("!WAYPOINT");
                    writer.WriteLine("#");
                    writer.WriteLine("!IMAGE:circle.png,");
                    
                    line = (string[])route[0];
                    lineToWrite = string.Concat(line[0], ",", line[1], ",,", line[3].Replace(",", ";"), ",");
                    writer.WriteLine(lineToWrite);

                    for (int i = 1; i < route.Count; i++)
                    {
                        line = (string[])route[i];
                        lineToWrite = string.Concat(line[0], ",", line[1], ",");

                        if (!string.IsNullOrEmpty(line[2]) ||
                            !line[2].ToLower().Equals("null"))
                        {
                            lineToWrite = string.Concat(lineToWrite, "GO ", line[2], distanceUnit);
                        }

                        lineToWrite = string.Concat(lineToWrite, ",", line[3].Replace(",", ";"), ",");

                        writer.WriteLine(lineToWrite);
                    }

                    MessageBox.Show(this.MdiParent, "Saved POI File Successfully", "Save Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Concat("Error: ", ex.Message),
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }

            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveFile();
        }
    }
}